package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class Testcontroller {

    @GetMapping("/abc")
    public String up() {
        return "test";
    }

    @GetMapping("/")
    public String registerpage() {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, String> register(Customer customer) {
        System.out.println(customer);
        Map<String, String> map = new HashMap<>();
        map.put("msg", "注册成功！");
        return map;
    }

//    @PostMapping("/login")
//    @ResponseBody
//    public User register(User user) {
//        System.out.println(user);
////        System.out.println(user.getPassword());
//        return user;
//    }

    @Autowired
    private ListService listService;
    @GetMapping("/list")
    public String list() {
        return "list";
    }

    @GetMapping("getlist")
    @ResponseBody
    public MyResult getlist(ArticleVO articleVO) {
        if (articleVO.getPage() == 0) {
            articleVO.setPage(1);
        }
        int b = (articleVO.getPage() - 1) * articleVO.getLimit();
        articleVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Artical> getpage = listService.getpage(articleVO);
        Integer getcount = listService.getcount(articleVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,getpage);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;

    }

    @GetMapping("/delete")
    @ResponseBody
    public Integer delete(Integer id) {
        Integer integer = listService.delete(id);
        return integer;
    }

    //内容详情
    @PostMapping("/tanchuid/{id}")
    @ResponseBody
    public Map<String, Object> tanchu(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Artical tanchu = listService.tanchu(id);
        map.put("code", 0);
        map.put("msg", "注册成功！");
        map.put("count", 1);
        map.put("data", tanchu);
        return map;

    }

    @PostMapping("/update")
    @ResponseBody
    public Map gengxin(Artical artical){
        Map map = new HashMap();
//        int update = listService.update(artical);
        int update = listService.update1(artical);
        map.put("code", 0);
        map.put("msg", "更新成功！");
        map.put("count", 1);
        map.put("data", update);
        return map;
    }

    @GetMapping("/Bydelete")
    @ResponseBody
    public Map<String,Object> Bydelete(Integer[] ids){
        Map<String, Object> map = new HashMap<>();
        Integer re = null;
//        for (Integer i: ids) {
            re = listService.Bydelete(ids);
//        }
        map.put("code", 0);
        map.put("msg", "请求成功！");
        map.put("count", 1);
        map.put("data", re);
        return map;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Map<String,Object> insert(Artical artical){
        Map<String, Object> map = new HashMap<>();
        Integer integer = listService.insert(artical);
        map.put("code", 0);
        map.put("msg", "请求成功！");
        map.put("count", 1);
        map.put("data", integer);
        return map;
    }

    @PostMapping("/search")
    @ResponseBody
    public Map<String,Object> search(Artical artical){
        Map<String, Object> map = new HashMap<>();
        Integer re= null;
        List<Artical> reArticles = listService.search(artical);
        map.put("code", 0);
        map.put("msg", "查询成功！");
        map.put("count", 1);
        map.put("data", reArticles);
        return map;
    }

    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(Artical artical,Shouye shouye){
        Map<String, Object> map = new HashMap<>();
        Artical login=listService.login(artical);
        listService.dianjiliang(shouye);
        map.put("code", 0);
        map.put("msg", "登陆成功！");
        map.put("count", 1);
        map.put("data", login);
        return map;
    }

    @GetMapping("getlist1")
    @ResponseBody
    public MyResult getlist1(ArticleVO articleVO) {
        if (articleVO.getPage() == 0) {
            articleVO.setPage(1);
        }
        int b = (articleVO.getPage() - 1) * articleVO.getLimit();
        articleVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Artical> getpage1 = listService.getpage1(articleVO);
        Integer getcount1 = listService.getcount1(articleVO);

        MyResult result = new MyResult(0,"注册成功！",getcount1,getpage1);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;

    }



    @GetMapping("/updateclose")
    @ResponseBody
    public int updateclose(Artical artical){
        return listService.updateclose(artical);
    }

}
